package com.zbkj.service.dividend.controller;

import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.utils.RedisUtil;
import com.zbkj.service.dividend.model.DividendPoolConfig;
import com.zbkj.service.dividend.request.DividendPoolConfigRequest;
import com.zbkj.service.dividend.service.DividendPoolConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 分红档位配置 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/dividendPool/level")
@Api(tags = "分红管理 -- 分红档位")
public class DividendPoolConfigController {

    @Autowired
    private DividendPoolConfigService dividendPoolConfigService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 分页显示分红档位配置
     * @param pageParamRequest 分页参数
     */
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<DividendPoolConfig>> getList(@Validated PageParamRequest pageParamRequest) {
        CommonPage<DividendPoolConfig> dividendPoolConfigCommonPage = CommonPage.restPage(dividendPoolConfigService.getList(pageParamRequest));
        return CommonResult.success(dividendPoolConfigCommonPage);
    }

    /**
     * 获取所有分红档位配置（不分页）
     */
    @ApiOperation(value = "全量列表")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public CommonResult<java.util.List<DividendPoolConfig>> getAll() {
        return CommonResult.success(dividendPoolConfigService.getAllActiveConfigs());
    }

    /**
     * 新增分红档位配置
     * @param dividendPoolConfigRequest 新增参数
     */
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated DividendPoolConfigRequest dividendPoolConfigRequest) {
        if (dividendPoolConfigService.create(dividendPoolConfigRequest)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 删除分红档位配置
     * @param id Integer
     */
    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        if (dividendPoolConfigService.removeById(id)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 修改分红档位配置
     * @param id integer id
     * @param dividendPoolConfigRequest 修改参数
     */
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestParam Integer id, @RequestBody @Validated DividendPoolConfigRequest dividendPoolConfigRequest) {
        if (dividendPoolConfigService.edit(id, dividendPoolConfigRequest)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 查询分红档位配置信息
     * @param id Integer
     */
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public CommonResult<DividendPoolConfig> info(@RequestParam(value = "id") Integer id) {
        return CommonResult.success(dividendPoolConfigService.getById(id));
    }

    /**
     * 启用/禁用分红档位
     * @param params 参数
     */
    @ApiOperation(value = "启用/禁用")
    @RequestMapping(value = "/use", method = RequestMethod.POST)
    public CommonResult<String> use(@RequestBody Map<String, Object> params) {
        Integer id = (Integer) params.get("id");
        Boolean isActive = (Boolean) params.get("isActive");
        redisUtil.delete("dividendConfig","dividend:".concat(String.valueOf(id)));
        if (dividendPoolConfigService.updateStatus(id, isActive)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 检查分红档位是否存在
     * @param level 档位金额
     * @param excludeId 排除的ID
     */
    @ApiOperation(value = "检查档位是否存在")
    @RequestMapping(value = "/checkLevel", method = RequestMethod.GET)
    public CommonResult<Boolean> checkLevel(@RequestParam Integer level,
                                            @RequestParam(required = false) Integer excludeId) {
        boolean exists = dividendPoolConfigService.checkLevelExists(level, excludeId);
        return CommonResult.success(exists);
    }
}